/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.cms.rpc.service.impl;

import java.util.Date;

import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.common.util.DateTimeUtils;
import com.lambkit.core.aop.AopKit;
import com.lambkit.db.sql.column.Example;
import com.lambkit.module.cms.common.CmsConsts;
import com.lambkit.module.cms.core.km.CmsScore;
import com.lambkit.module.cms.rpc.api.CmsAccessService;
import com.lambkit.module.cms.rpc.model.CmsAccess;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-02-25
 * @version 1.0
 * @since 1.0
 */
public class CmsAccessServiceImpl extends LambkitModelServiceImpl<CmsAccess> implements CmsAccessService {
	
	private CmsAccess DAO = null;
	
	public CmsAccess dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(CmsAccess.class);
		}
		return DAO;
	}

	@Override
	public boolean addArticleAccess(Long articleId, Long userId, String type) {
		// TODO Auto-generated method stub
		if(userId.equals(1L)) return false;
		CmsAccess access = new CmsAccess();
		access.setUserId(userId);
		access.setAccessTime(DateTimeUtils.getCurrentTime());
		access.setRefid(articleId);
		access.setReftype(CmsConsts.RefType.Article.value());
		access.setAccessType(type);
		boolean flag = access.save();
		if(flag) CmsScore.service().addArticleScore(userId, articleId, 1L);
		return flag;
	}

	@Override
	public boolean addFileAccess(Long fileId, Long userId, String type) {
		// TODO Auto-generated method stub
		if(userId.equals(1L)) return false;
		CmsAccess access = new CmsAccess();
		access.setUserId(userId);
		access.setAccessTime(DateTimeUtils.getCurrentTime());
		access.setRefid(fileId);
		access.setReftype(CmsConsts.RefType.File.value());
		access.setAccessType(type);
		boolean flag = access.save();
		if(flag) CmsScore.service().addFileScore(userId, fileId, 1L);
		return flag;
	}

	@Override
	public Long addFileDownloadAccess(Long fileId, Long userId) {
		// TODO Auto-generated method stub
		if(userId.equals(1L)) return null;
		CmsAccess access = new CmsAccess();
		access.setUserId(userId);
		access.setAccessTime(DateTimeUtils.getCurrentTime());
		access.setRefid(fileId);
		access.setReftype(CmsConsts.RefType.File.value());
		access.setAccessType("download");
		boolean flag = access.save();
		if(flag) {
			CmsScore.service().addFileScore(userId, fileId, 1L);
			Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
			Example example = CmsAccess.sql().andUserIdEqualTo(userId).andAccessTypeEqualTo("download")
			.andReftypeEqualTo(CmsConsts.RefType.File.value()).andAccessTimeGreaterThan(today).example();
			example.setLoadColumns("count(*) as cnt");
			CmsAccess ca = findFirst(example);
			Long cnt = ca.getLong("cnt");
			return cnt;
		} else {
			return null;
		}
	}
}
